<template>
  <div class="add-box">
    <el-dialog width="600px" :title="dialogTitle" v-model="dialogFormVisible" :close-on-click-modal="false" :draggable="true"
      @close="dialogClose">
      <div>
        <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules">
          <el-row>
            <el-col :span="12">
              <el-form-item style="width:90%;" label="下游名称" prop="appname">
                <el-input v-model="dialogForm.appname" maxlength="16" placeholder="请输入下游名称" @keypress.enter="submitHandle" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item style="width:90%;" label="APPID" prop="appid">
                <el-input :disabled="dialogType == 'edit'" v-model="dialogForm.appid" maxlength="32" placeholder="请输入APPID"
                  @keypress.enter="submitHandle" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item style="width:90%;" label="IP地址" prop="allowIps">
                <el-input v-model="dialogForm.allowIps" placeholder="请输入IP地址" @keypress.enter="submitHandle" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item style="width:90%;" label="回调地址" prop="notifyUrl">
                <el-input v-model="dialogForm.notifyUrl" placeholder="请输入回调地址" @keypress.enter="submitHandle" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item style="width:90%;" label="状态" prop="flag">
                <el-radio-group v-model="dialogForm.flag">
                  <el-radio :label="1">启用</el-radio>
                  <el-radio :label="2">禁用</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitHandle">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
    <confirm-pass ref="confirmPassRef" @verification="postData" />
  </div>
</template>

<script>
import { reactive, ref, toRefs, watch } from 'vue'
import { successDeal, warningDeal } from '@/utils/utils'
import confirmPass from '@/components/confirmPass.vue'
import api from '../api'
export default {
  components: {
    confirmPass
  },
  setup(props, context) {
    const dialogFormRef = ref(null)
    const confirmPassRef = ref(null)

    const state = reactive({
      dialogTitle: '',
      dialogType: null,
      dialogFormVisible: false,
      incomingForm: {},
      dialogForm: {
        flag: 1,
      },
      dialogFormRules: {
        appname: [
          { required: true, message: '请输入下游名称', trigger: 'blur' },
        ],
        appid: [
          { required: true, message: '请输入APPID', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{8,}$/, message: '请输入8-32位且只包含数字、字母', trigger: 'blur'
          },
        ],
        allowIps: [
          { required: true, message: '请输入IP地址', trigger: 'blur' },
        ],
        flag: [
          { required: true, message: '请选择状态', trigger: 'blur' },
        ],
      },
    })

    watch(() => state.dialogType, (newVal, oldVal) => {
      if (newVal == 'add') {
        state.dialogTitle = '新增下游接口'
      } else if (newVal == 'edit') {
        state.dialogTitle = '编辑下游接口'
      }
    })

    function dialogShow(type, data) {
      state.dialogType = type
      if (type == 'edit') {
        getDetail(data.appid)
      }
      state.incomingForm = data
      state.dialogFormVisible = true
    }

    function getDetail(appid) {
      api.apiUserDetail({ appid }).then(res => {
        if (res.code == '200') {
          state.dialogForm = res.data
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function dialogClose() {
      state.dialogForm = {
        flag: 1,
      }
      dialogFormRef.value.resetFields()
    }

    function submitHandle() {
      dialogFormRef.value.validate((valid, fields) => {
        if (valid) {
          if (state.dialogType == 'add') {
            const title = '下游接口新增提示'
            const msg = '是否确认新增下游接口？'
            confirmPassRef.value.dialogShow(title, msg)
          } else {
            const title = '下游接口修改提示'
            const msg = '是否确认修改下游接口？'
            confirmPassRef.value.dialogShow(title, msg)
          }
        }
      })
    }

    function postData() {
      const obj = {
        ...state.dialogForm,
      }
      if (state.dialogType == 'add') {
        api.apiUserAdd(obj).then(res => {
          if (res.code == '200') {
            successDeal(res.msg)
            context.emit('dialogClose', true)
            state.dialogFormVisible = false
          } else {
            warningDeal(res.msg)
          }
        }).catch(err => {
          console.log(err)
        })
      } else {
        api.apiUserModify(obj).then(res => {
          if (res.code == '200') {
            successDeal(res.msg)
            context.emit('dialogClose', true)
            state.dialogFormVisible = false
          } else {
            warningDeal(res.msg)
          }
        }).catch(err => {
          console.log(err)
        })
      }
    }

    return {
      ...toRefs(state),
      dialogFormRef,
      confirmPassRef,
      dialogShow,
      submitHandle,
      dialogClose,
      postData,
    }
  }
}
</script>

<style>
</style>